from utils.DBService import sqlquery
import os

devsql = """select ts.departname,wzdb.banhezhanminchen from t_s_depart as ts,WZDibangxinxi as wzdb where wzdb.departid = ts.ID and ts.ID in (\'{}\')"""

DEP_SQL = """WITH  dept
    AS (SELECT  *
        FROM   t_s_depart
        WHERE  parentdepartid in ('{}')
        UNION ALL
        SELECT  d.*
        FROM   t_s_depart d
            INNER JOIN dept ON d.parentdepartid = dept.id
						 )
  SELECT id
  FROM  dept"""

TEST_SQL = """select * from t_s_depart where ID in (\'{}\')"""

def devStatic(Args,user):
    print('Dev参数:\n\t', Args)
    depId = Args['depId']
    # depId = 'f6323b406d19fc87016d1a5b43910012'
    depId = '\',\''.join(depId.split(','))
    tests = sqlquery(TEST_SQL.format(depId))
    print('TESTS:\n\t', tests.__len__())
    if tests.__len__() == 0:
        depId = 'All'
    if depId == 'All':
        deps = sqlquery(DEP_SQL.format(user))
    else:
        depId = '\',\''.join(depId.split(','))
        deps = sqlquery(DEP_SQL.format(depId))
    print('SQL:\n\t', deps)
    print(deps)
    depsid = []
    for dep in deps:
        depsid.append(dep[0])
    depId = '\',\''.join(depsid)
    cailiaoData = sqlquery(devsql.format(depId))
    print(devsql.format(depId))
    clData = [['站点', '设备']]
    restitle = '设备信息如下'
    resdata = []
    site = {'title': '', 'data': {
        'tableHead': [{
            'title': '站点',
            'dataIndex': 'zhandian',
            'key': 'zhandian',
        },
        {
            'title': '设备',
            'dataIndex': 'shebei',
            'key': 'shebei',
        }],
        'tableBody': []
    }}
    title = {'key': 0, 'zhandian': '', 'shebei': ''}
    keyindex = 0
    for cailiao in cailiaoData:
        if os.environ.get("ENV_NAME") == '1':
            cailiao1 = cailiao[1]
        else:
            cailiao1 = cailiao[1].encode('latin1').decode('gbk')
        clData.append([
            cailiao[0],
            cailiao1
        ])
        keyindex = keyindex + 1
        title['key'] = keyindex
        title['zhandian'] = cailiao[0]
        title['shebei'] = cailiao1
        site['data']['tableBody'].append(title)
        title = {'key': 0, 'zhandian': '', 'shebei': ''}
    resdata.append(site)
    print(clData)
    return clData,resdata,restitle

functionConf = {
    "name": "devStatic",
    "description": "Query the equipment and site information based on the depart ID, and display the corresponding equipment information for the site. Be careful to understand the scale as geomagnetic",
    "parameters": {
        "type": "object",
        "properties": {
            # "site": {
            #     "type": "string",
            #     "description": "Department names, multiple departments separated by English commas, default to All，eg：A项目1标拌合站,A项目2标拌合站",
            # },
            # "device": {
            #     "type": "string",
            #     "description": "Device names, multiple devices separated by English commas, default to All, eg:1标地磅,2标地磅"
            # }
            "depId": {
                "type": "string",
                "description": "Department IDs, multiple departments separated by commas in English, default is All",
            }
        },
        "required": [
            "depId"
        ]
    }
}